﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;


namespace RStudio.InputManager
{
    /// <summary>
    /// 鼠标交互组件（处理鼠标相关输入事件）
    /// </summary>
    public class MouseInteraction : MonoBehaviour,
        IInteraction,
        IPointerClickHandler,
        IPointerDownHandler, IPointerUpHandler,
        IPointerEnterHandler, IPointerExitHandler, IPointerMoveHandler,
        IDragHandler, IBeginDragHandler, IEndDragHandler
    {

        #region 公共事件
        public event Action<PointerEventData> OnClick;
        public event Action<PointerEventData> OnUp;
        public event Action<PointerEventData> OnDown;

        public event Action<PointerEventData> OnEnter;
        public event Action<PointerEventData> OnExit;
        public event Action<PointerEventData> OnMove;

        public event Action<PointerEventData> OnDragBegin;
        public event Action<PointerEventData> OnDragging;
        public event Action<PointerEventData> OnDragEnd;

        public event Action OnStay;
        #endregion

        public void SetEnabled(bool enabled)
        {
            CanvasGroup canvasGroup = GetComponent<CanvasGroup>()
                                      ?? gameObject.AddComponent<CanvasGroup>();

            // 控制是否响应射线检测（即是否启用交互）
            canvasGroup.blocksRaycasts = enabled; 
        }
        public void Dispose()
        {
            OnClick = null;
            OnUp = null;
            OnDown = null;
            OnEnter = null;
            OnExit = null;
            OnMove = null;
            OnDragBegin = null;
            OnDragging = null;
            OnDragEnd = null;
            OnStay = null;
        }


        #region Unity 内置接口
        public void OnPointerClick(PointerEventData eventData) => OnClick?.Invoke(eventData);
        public void OnPointerUp(PointerEventData eventData) => OnUp?.Invoke(eventData);
        public void OnPointerDown(PointerEventData eventData) => OnDown?.Invoke(eventData);

        public void OnPointerEnter(PointerEventData eventData) => OnEnter?.Invoke(eventData);
        public void OnPointerExit(PointerEventData eventData) => OnExit?.Invoke(eventData);
        public void OnPointerMove(PointerEventData eventData) => OnMove?.Invoke(eventData);

        public void OnBeginDrag(PointerEventData eventData) => OnDragBegin?.Invoke(eventData);
        public void OnDrag(PointerEventData eventData) => OnDragging?.Invoke(eventData);
        public void OnEndDrag(PointerEventData eventData) => OnDragEnd?.Invoke(eventData);

        private void OnMouseOver() => OnStay?.Invoke();

        #endregion
    }
}